package com.quotientintelligence.caftrak.shared;

import java.util.*;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;


@PersistenceCapable
public class Substance
{
	public enum ActiveIng { Caffeine, Nicotene, Sugar }

	public enum MUnits 
	{
		gram(1),
		milligram(1000),
		microgram(1000000);

		private static final Map<Integer,MUnits> lookup 
		      = new HashMap<Integer,MUnits>();
		
		static {
		     for(MUnits s : EnumSet.allOf(MUnits.class))
		          lookup.put(s.getCode(), s);
		}
		
		private int code;
		
		private MUnits(int code) {
		     this.code = code;
		}
		
		public int getCode() { return code; }
		
		public static MUnits get(int code) { 
		     return lookup.get(code); 
		}
	}
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key _key;
	
	@Persistent
	private ActiveIng _substanceType;

	@Persistent
	private int _quantity;	

	@Persistent
	private MUnits _quantUnits;

	@Persistent
	public String Name;
	
	public Substance(ActiveIng type)
	{
		_substanceType=type;
	}
	
	public Substance(String name)
	{
		Name = name;
	}
	public Substance(String name, ActiveIng type)
	{
		this(name);
		_substanceType=type;
	}	
	public Substance(String name, ActiveIng type, int quantity, MUnits units)
	{
		this(name,type);
		_quantity = quantity;
		_quantUnits = units;
	}	
	public Substance(ActiveIng type, int quantity, MUnits units)
	{
		this("",type,quantity,units);
	}

	public MUnits get_quantUnits() {
		return _quantUnits;
	}
	public int get_quantity() {
		return _quantity;
	}
	public float get_quantity(MUnits m) {
		return get_quantityInUnits(m);
	}
	public void set_quantity(int value) {
		_quantity = value;
	}
	public void set_quantity(int value, MUnits m)
	{
		_quantity = value;
		_quantUnits = m;
	}
	
	public float get_quantityInUnits(MUnits m)
	{
		// take the ratio of requested over stored, and you've got a multiplier.
		return _quantity*( m.getCode()/_quantUnits.getCode());
	}
	
	public ActiveIng get_substanceType() {
		return _substanceType;
	}
	public Key getKey() {
	       return _key;
	}
}
